﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using myTwitter.BO;

namespace myTwitter.Converters
{
    public class ElapsedTimeConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime createdAt = (DateTime)value;
            TimeSpan elapsedTime = DateTime.Now.ToUniversalTime() - createdAt;
            if (elapsedTime.Days > 365)
            {
                if (elapsedTime.Days == 365)
                    return "1 year ago";
                else
                    return System.Convert.ToInt32(elapsedTime.Days / 365).ToString() + " years ago";
            }
            if (elapsedTime.Days > 0 && elapsedTime.Days<365)
            {
                if (elapsedTime.Days == 1)
                    return elapsedTime.Days.ToString() + " day ago";
                else
                    return elapsedTime.Days.ToString() + " days ago";
            }
            if (elapsedTime.Hours > 0)
            {
                if (elapsedTime.Hours == 1)
                    return elapsedTime.Hours.ToString() + " hour ago";
                else
                    return elapsedTime.Hours.ToString() + " hours ago";
            }
            if (elapsedTime.Minutes > 0)
            {
                if (elapsedTime.Minutes == 1)
                    return elapsedTime.Minutes.ToString() + " minute ago";
                else
                    return elapsedTime.Minutes.ToString() + " minutes ago";
            }
            else
            {
                if (elapsedTime.Seconds == 1)
                    return elapsedTime.Seconds.ToString() + " second ago";
                else
                    return elapsedTime.Seconds.ToString() + " seconds ago";                
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
